/**
 * 
 */
package au.edu.swin.videoshop.persistence;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import au.edu.swin.videoshop.bean.Address;
import au.edu.swin.videoshop.bean.AddressState;

/**
 * @author julie
 *
 */
@Entity
@NamedQueries({
	@NamedQuery (
			name="findAllStaffs", 
			query="SELECT s FROM StaffEntity s WHERE s.deleted = FALSE"
	),
	@NamedQuery (
			name="findStaffsByStoreId", 
			query="SELECT s FROM StaffEntity s WHERE s.deleted = FALSE AND s.store.storeId=:storeId"
	),
	@NamedQuery (
			name="authenticateStaff", 
			query="SELECT s FROM StaffEntity s WHERE s.deleted = FALSE AND s.userName=:userName AND s.password=:password"
	),
	@NamedQuery (
			name="changeStaffPassword", 
			query="UPDATE StaffEntity s SET s.password=:password WHERE s.deleted = FALSE AND s.userName=:userName AND s.password=:currentPassword"
	),
	@NamedQuery (
			name="changeStaffRole", 
			query="UPDATE StaffEntity s SET s.role=:staffRole WHERE s.deleted = FALSE AND s.staffId=:staffId"
	)
})
@Table(name="STAFF")
public class StaffEntity implements Serializable{
	private Long mStaffId;
	private StaffEntity mSupervisor;
	private String mFirstName;
	private String mLastName;
	private String mContactNumber;
	private String mEmailAddress;
	private String mUserName;
	private String mPassword;
	private String mRole;
	private StoreEntity mStore;
	private Address mAddress;

	
	//Attribute to mark the entity as deleted
	private boolean mDeleted;
	private String mDeleteInfo;
	
	public StaffEntity() {
		mDeleted = false;
	}

	/**
	 * @return the staffId
	 */
	@Id
	@GeneratedValue (strategy=GenerationType.AUTO)
	@Column(name="STAFF_ID")
	public Long getStaffId() {
		return mStaffId;
	}

	/**
	 * @param staffId the staffId to set
	 */
	public void setStaffId(Long staffId) {
		mStaffId = staffId;
	}
	
	/**
	 * @return the userName
	 */
	@Column(name="USER_NAME", length=20, unique=true, nullable=false)
	public String getUserName() {
		return mUserName;
	}
	
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		mUserName = userName;
	}

	/**
	 * @return the password
	 */
	@Column(name="PASSWORD", length=20)
	public String getPassword() {
		return mPassword;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		mPassword = password;
	}
	
	/**
	 * @return the role
	 */
	@Column(name="STAFF_ROLE", length=20)
	public String getRole() {
		return mRole;
	}

	/**
	 * @param role the role to set
	 */
	public void setRole(String role) {
		mRole = role;
	}

	/**
	 * @return the store
	 */
	@ManyToOne(optional=true, fetch=FetchType.EAGER)
	@JoinColumn(name="STORE_ID")
	public StoreEntity getStore() {
		return mStore;
	}

	/**
	 * @param store the store to set
	 */
	public void setStore(StoreEntity store) {
		mStore = store;
	}
	/**
	 * @return the supervisor
	 */
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="SUPERVISOR_ID")
	public StaffEntity getSupervisor() {
		return mSupervisor;
	}

	/**
	 * @param supervisor the supervisor to set
	 */
	public void setSupervisor(StaffEntity supervisor) {
		mSupervisor = supervisor;
	}

	/**
	 * @return the firstName
	 */
	@Column(name="FIRST_NAME", length=20)
	public String getFirstName() {
		return mFirstName;
	}

	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		mFirstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	@Column(name="LAST_NAME", length=20)
	public String getLastName() {
		return mLastName;
	}

	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		mLastName = lastName;
	}

	/**
	 * @return the contactNumber
	 */
	@Column(name="CONTACT_NUMBR", length=20)
	public String getContactNumber() {
		return mContactNumber;
	}

	/**
	 * @param contactNumber the contactNumber to set
	 */
	public void setContactNumber(String contactNumber) {
		mContactNumber = contactNumber;
	}

	/**
	 * @return the emailAddress
	 */
	@Column(name="EMAIL_ADDRESS", length=50)
	public String getEmailAddress() {
		return mEmailAddress;
	}

	/**
	 * @param emailAddress the emailAddress to set
	 */
	public void setEmailAddress(String emailAddress) {
		mEmailAddress = emailAddress;
	}
	
	/**
	 * @return
	 * @see au.edu.swin.videoshop.bean.Address#getAddressLine()
	 */
	@Column(name="ADDRESS_LINE", length=200)
	public String getAddressLine() {
		return mAddress.getAddressLine();
	}

	/**
	 * @return
	 * @see au.edu.swin.videoshop.bean.Address#getPostcode()
	 */
	@Column(name="POSTCODE", length=10)
	public String getPostcode() {
		return mAddress.getPostcode();
	}

	/**
	 * @return
	 * @see au.edu.swin.videoshop.bean.Address#getState()
	 */
	@Column(name="STATE", length=10)
	public String getState() {
		return mAddress.getState().getName();
	}

	/**
	 * @return
	 * @see au.edu.swin.videoshop.bean.Address#getSuburb()
	 */
	@Column(name="SUBURB", length=200)
	public String getSuburb() {
		return mAddress.getSuburb();
	}

	/**
	 * @param addressLine1
	 * @see au.edu.swin.videoshop.bean.Address#setAddressLine(java.lang.String)
	 */
	public void setAddressLine(String addressLine) {
		if (mAddress == null) {
			mAddress = new Address();
		}
		mAddress.setAddressLine(addressLine);
	}

	/**
	 * @param postcode
	 * @see au.edu.swin.videoshop.bean.Address#setPostcode(java.lang.String)
	 */
	public void setPostcode(String postcode) {
		if (mAddress == null) {
			mAddress = new Address();
		}
		mAddress.setPostcode(postcode);
	}

	/**
	 * @param state
	 * @see au.edu.swin.videoshop.bean.Address#setState(java.lang.String)
	 */
	public void setState(String state) {
		if (mAddress == null) {
			mAddress = new Address();
		}
		mAddress.setState(AddressState.getInstance(state));
	}

	/**
	 * @param suburb
	 * @see au.edu.swin.videoshop.bean.Address#setSuburb(java.lang.String)
	 */
	public void setSuburb(String suburb) {
		if (mAddress == null) {
			mAddress = new Address();
		}
		mAddress.setSuburb(suburb);
	}

	/**
	 * @return the address
	 */
	@Transient
	public Address getAddress() {
		return mAddress;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(Address address) {
		mAddress = address;
	}
	
	/**
	 * @return the deleted
	 */
	@Column(name="IS_DELETED")
	public boolean isDeleted() {
		return mDeleted;
	}
	
	/**
	 * @param deleted the deleted to set
	 */
	public void setDeleted(boolean deleted) {
		mDeleted = deleted;
	}
	
	/**
	 * @return the deleteInfo
	 */
	@Column(name="DELETE_INFO")
	public String getDeleteInfo() {
		return mDeleteInfo;
	}
	
	/**
	 * @param deleteInfo the deleteInfo to set
	 */
	public void setDeleteInfo(String deleteInfo) {
		mDeleteInfo = deleteInfo;
	}
}

